﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;

namespace AutoExecuteSp.AutoExecuteSp
{
    public class Program
    {
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool AllocConsole();

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool FreeConsole();

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool AttachConsole(IntPtr pid);

        private static void Main(string[] args)
        {
            AssociateEqsFile associateEqsFile = new AssociateEqsFile();
            associateEqsFile.CreateFileAssociation(".eqs", "EQSFile", "Auto Script Result Files");

            if (args.Length > 0)
            {
                FreeConsole();
                MainForm mainForm = new MainForm();
                Application.Run(mainForm);
            }
            else
            {
                try
                {
                    //AttachConsole(new IntPtr(ParentProcess.ProcessId));

                    AutoScript autoScript = new AutoScript();

                    autoScript.Run();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                }
                Console.WriteLine("Auto Script End");
            }
        }
    }
}